// pages/menberCard/menberCard.js
import {
  postRequest,
  getRequest
} from '../../api/request';
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeColor: app.globalData.themeColor,
    subColor: app.globalData.subColor,
    btnColor: app.globalData.btnColor,
    pageType: '',
    home: '/pages/index1/index1',
    title: '等级会员',
    interval: 1000,
    autoplay: false,
    duration: 500,
    indicatorDots: true,
    current: 0,
    level: 0,
    currentConsume: 0,//当前等级
    share_num: 0,
    globalData: app.globalData,
    maxVipLevelShow: 4,
    become_vip_requirement: [],
    showStyle: 0,
  },

  //轮播改变时
  bindchange(e) {
    this.setData({
      current: e.detail.current
    })
  },

  //获取等级
  getMemberLevel() {
    let that = this;
    getRequest("/api/vip/member/getMemberLevel", {}).then((res) => {
      console.log(res.data.data);
      if (res.data.data != null) {
        this.setData({
          become_vip_requirement: res.data.data,
        })
        this.getUserBaseInfo();
        //是否无数据
        if(res.data.data.length == 0){
          this.setData({
            showStyle: 2,
          });
        }else{
          this.setData({
            showStyle: 0,
          });
        }
      }
      // 停止下拉刷新效果
      wx.stopPullDownRefresh()
    }).catch((err) => {
      // 停止下拉刷新效果
      wx.stopPullDownRefresh()
    });
  },
  //检测用户在那个等级
  checkUserCurrent(level) {
    let current = 0;
    let currentConsume = 0;
    this.data.become_vip_requirement.forEach((item, index) => {
      if (level == item.id) {
        current = index;
        currentConsume = item.consume
      }
    });
    this.setData({
      current,
      currentConsume,
    })
  },

  //获取个人信息
  getUserBaseInfo() {
    let that = this;
    getRequest("/api/vip/getUserBaseInfo", {
      memberId: app.myInfo.memberId
    }).then((res) => {
      console.log(res.data.data);
      if (res.data.data != null) {
        that.checkUserCurrent(res.data.data.level, 1)
        that.setData({
          level: res.data.data.consumeMoney,
        })
      }
    }).catch((err) => {});
  },

  //前往充值页面
  navRecharge(){
    wx.navigateTo({
      url: '/pages/storedValue/storedValue',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    //如果没有token,则需要重新登录获取个人信息等，避免缺少id导致数据无法获取
    if (app.token == "" || app.token == undefined) {
      app.initLogin().then(res => {
        this.getMemberLevel();
      }).catch(err => {})
    } else {
      this.getMemberLevel();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.getMemberLevel();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})